package com.zijie.hosptal.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.hosptal.aop.AutoFill;
import com.zijie.hosptal.aop.OperationType;
import com.zijie.hosptal.mapper.PatientMapper;
import com.zijie.hosptal.pojo.po.Patient;
import com.zijie.hosptal.service.PatientService;
import com.zijie.hosptal.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {

    private PatientMapper patientMapper;

    @Autowired
    public void setPatientMapper(PatientMapper patientMapper) {
        this.patientMapper = patientMapper;
    }

    @Override
    public Page<Patient> list(Page<Patient> patientPage, Patient patient) {
        Page<Patient> p=patientMapper.findList(patientPage,patient);
        return p;
    }

    @AutoFill(OperationType.INSERT)
    @Override
    public boolean save(Patient patient) {
        patient.setPassword(Md5Util.getMD5String("123"));
        return patientMapper.insert(patient)>0;
    }

    @AutoFill(OperationType.UPDATE)
    @Override
    public boolean updateById(Patient patient) {
        if (patient.getPassword() != null && !patient.getPassword().isEmpty()){
            patient.setPassword(Md5Util.getMD5String(patient.getPassword()));
        }
        return patientMapper.updateByPatient(patient) > 0;
    }

    @Override
    public Patient getById(Integer id) {
        return patientMapper.selectById(id);
    }

    @Override
    public List<Patient> getAllPatient() {
        return patientMapper.getAllPatient();
    }


}
